package com.ruoyi.aqsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.aqsc.domain.AShopManage;
import com.ruoyi.aqsc.mapper.AShopManageMapper;
import com.ruoyi.aqsc.service.AShopManageService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;


@Service
@Primary
public class AShopmanageServiceImpl extends ServiceImpl<AShopManageMapper,AShopManage> implements AShopManageService {

   @Autowired
   private AShopManageMapper aShopManageMapper;
   @Autowired
   Validator validator;


   @Override
   public String importame(List<AShopManage> userList, boolean updateSupport) {
         if (StringUtils.isNull(userList) || userList.size() == 0)
         {
            throw new ServiceException("导入用户数据不能为空！");
         }
         int successNum = 0;
         int failureNum = 0;
         StringBuilder successMsg = new StringBuilder();
         StringBuilder failureMsg = new StringBuilder();
         for (AShopManage card : userList)
         {
            try
            {
               // 验证是否存在这个用户
               LambdaQueryWrapper<AShopManage> wrapper=new LambdaQueryWrapper<>();
               wrapper.eq(AShopManage::getScenePhotos,card.getScenePhotos());
               AShopManage u = aShopManageMapper.selectOne(wrapper);

               if (StringUtils.isNull(u))
               {
                  BeanValidators.validateWithException(validator, card);
                  aShopManageMapper.insert(card);
                  successNum++;
                  successMsg.append("<br/>" + successNum + "、账号 " + card.getScenePhotos() + " 导入成功");
               }
               else if (updateSupport)
               {
                  BeanValidators.validateWithException(validator, card);
                  aShopManageMapper.update(card,wrapper);
                  successNum++;
                  successMsg.append("<br/>" + successNum + "、账号 " + card.getScenePhotos() + " 更新成功");
               }
               else
               {
                  failureNum++;
                  failureMsg.append("<br/>" + failureNum + "、账号 " + card.getScenePhotos() + " 已存在");
               }
            }
            catch (Exception e)
            {
               failureNum++;
               String msg = "<br/>" + failureNum + "、账号 " + card.getScenePhotos() + " 导入失败：";
               failureMsg.append(msg + e.getMessage());
               log.error(msg, e);
            }
         }
         if (failureNum > 0)
         {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
         }
         else
         {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
         }
         return successMsg.toString();
      }
}









